#include "GLui.h"
#include <math.h>


TTFFont *g_GLuiFont = NULL;
Texture *g_GLuiCursorTex = NULL;

void GLui::drawCursor(int x, int y, GLuint texid)
{
    //DRAW CURSOR
    int verts[] = {x, y-32, x, y, x+32, y, x+32, y-32}; 
    float tex[] = {0, 1, 0, 0, 1, 0, 1, 1};

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//    glAlphaFunc(GL_GREATER, 0.7f);

    //select cursor texture based on z value;
    glBindTexture(GL_TEXTURE_2D,texid);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2, GL_INT, 0, verts);
    glTexCoordPointer(2, GL_FLOAT, 0, tex);
    glDrawArrays(GL_QUADS, 0, 4);
    
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_BLEND);
    ////////////////////////////////
}

void GLRect::move(int x, int y)
{
    this->x = x;
    this->y = y;
    genRectCoords();
}
void GLRect::genRectCoords()
{
    float theta = 0;
    float incr = 2 * 3.14f/GLRect::NumVerts;
    int index = 0;
    int numVertPerCorner = GLRect::NumVerts / 4;

    ////top right corner
    for(int i = 0; i < numVertPerCorner; i++, theta+= incr, index++)
    {
        _v[2*index] = (x + w - r) +  r * cos(theta);
        //_v[2*index+1] = (y + h - r) + r * sin(theta);
        _v[2*index+1] = (y - r) + r * sin(theta);
    }
    //top left corner
    for(int i = 0; i < numVertPerCorner; i++, theta+=incr, index++)
    {
        _v[2*index] = (x + r) +  r * cos(theta);
        _v[2*index+1] = (y - r) + r * sin(theta);
    }

    ////bottom left corner
    for(int i = 0; i < numVertPerCorner; i++, theta+=incr, index++)
    {
        _v[2*index] = (x + r) +  r * cos(theta);
        _v[2*index+1] = (y -h + r) + r * sin(theta);
    }

    ////bottom right corner
    for(int i = 0; i < numVertPerCorner; i++, theta+= incr, index++)
    {
        _v[2*index] = (x + w - r) +  r * cos(theta);
        _v[2*index+1] = (y -h + r) + r * sin(theta);
    }

}

GLRect::GLRect(int x, int y, int w, int h, int r)
{
    this->x = x;
    this->y = y;
    this->w = w;
    this->h = h;
    this->r = r;
    col[0] = col[1] = col[2] = col[3] = 1;

    _v = new float[GLRect::NumVerts * 2]; //2 because it is 2D
    genRectCoords();
}

void GLRect::draw()
{
    glColor4f(col[0], col[1], col[2], col[3]);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, _v);
    glDrawArrays(GL_TRIANGLE_FAN, 0, GLRect::NumVerts);
    glDisableClientState(GL_VERTEX_ARRAY);
}
bool GLRect::isPointOver(int mx, int my)
{
    if(mx > x && mx < x+w)
    {
        if(my > y - h && my < y)
        {
           return true;
        }
    }
    return false;
}
void GLRect::setColor(int r, int g, int b, int a)
{
    col[0] = r/255.0f;
    col[1] = g/255.0f;
    col[2] = b/255.0f;
    col[3] = a/255.0f;
    //glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
}

GLRect::~GLRect()
{
    delete [] _v;
}
